﻿// Copyright (c) egmkang wang. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Gateway
{
    using System;
    using System.Collections.Concurrent;
    using System.Threading.Tasks;
    using Orleans;
    using Orleans.Concurrency;
    using DotNetty.Transport.Channels;
    using XServer.GrainInterfaces;
    using XServer.SDK.Net;
    using XServer.SDK;
    
    public sealed class PlayerContainer
    {
        public static void AddPlayer(long playerID, IChannel channel)
        {
            if (channel == null) return;
            WeakReference c = null;
            players.TryRemove(playerID, out c);
            players.TryAdd(playerID, new WeakReference(channel));
        }

        public static IChannel GetPlayer(long playerID)
        {
            WeakReference c = null;
            players.TryGetValue(playerID, out c);
            return c != null ? c.Target as IChannel : null;
        }

        static ConcurrentDictionary<long, WeakReference> players = new ConcurrentDictionary<long, WeakReference>();
    }
}
